package com.mjr.webmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mjr.webmanage.mapper.EmpExprMapper;
import com.mjr.webmanage.mapper.EmpLogMapper;
import com.mjr.webmanage.mapper.EmpMapper;
import com.mjr.webmanage.pojo.*;
import com.mjr.webmanage.service.EmpLogService;
import com.mjr.webmanage.service.EmpService;
import com.mjr.webmanage.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogMapper empLogMapper;
    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Emp> emps = empMapper.list(page, pageSize);
        Page<Emp> p = (Page<Emp>) emps;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public PageBean page2(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        List<Emp> emps = empMapper.list2(empQueryParam);
        Page<Emp> p = (Page<Emp>) emps;
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Transactional(rollbackFor = {Exception.class}) // 所有异常都回滚
    @Override
    public void add(Emp emp) {

        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            empMapper.insert(emp);

//            int i = 1 / 0;
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                exprList.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });
            }

//        log.info("================" + exprList);

            empExprMapper.insertBatch(exprList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            empLogService.insertLog(new EmpLog(0, LocalDateTime.now(), "新增员工" + emp));
        }


    }

    @Transactional
    @Override
    public void delete(List<Integer> ids) {

        empExprMapper.deleteByEmpIds(ids);
        // 批量删除 基本信息
        empMapper.deleteByIds(ids);
        // 批量删除工作经历


    }

    @Override
    public Emp getInfo(Integer id) {


        return empMapper.getById(id);
    }

    @Transactional
    @Override
    public void update(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);


        empExprMapper.deleteByEmpIds(Collections.singletonList(emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e = empMapper.getByUsernameAndPassword(emp);
        if (e != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", e.getId());
            map.put("username", e.getUsername());
            map.put("name", e.getName());
            String token = JwtUtils.generateJwt(map);
            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), token);
        }
        return null;
    }
}
